// weakmap.js
const objNum = 10 * 1024 * 1024;
const useType = 2; // 修改 useType 值来测试Map和WeakMap
const curType = useType == 1 ?"【Map】" : "【WeakMap】";
let arr = new Array(objNum);

function usageSize() {
    const used = process.memoryUsage().heapUsed;
    return Math.round((used / 1024 / 1024) * 100) / 100 + "M";
}

if (useType == 1) {
    global.gc();
    console.log(objNum + '个' + curType + '占用内存：' + usageSize());

    const map = new Map();
    map.set(arr, 1);

    global.gc();
    console.log(objNum + '个' + curType + '占用内存：' + usageSize());

    arr = null;
    global.gc();
    console.log(objNum + '个' + curType + '占用内存：' + usageSize());

    console.log("=====")
} else {
    global.gc();
    console.log(objNum + '个' + curType + '占用内存：' + usageSize());

    const map = new WeakMap();

    global.gc();
    console.log(objNum + '个' + curType + '占用内存：' + usageSize());

    arr = null;
    global.gc();
    console.log(objNum + '个' + curType + '占用内存：' + usageSize());

    console.log("=====")
}